import _ from "lodash";

const dpiInfo = {};

//调用直接 new unitConversion().pxConversionMm(像素)
//        new unitConversion().mmConversionPx (毫米)


function conversion_getDPI() {
  if (window.screen.deviceXDPI) {
    dpiInfo.dpix = window.screen.deviceXDPI;
    dpiInfo.dpiy = window.screen.deviceYDPI;
  } else {
    var tmpNode = document.createElement("DIV");
    tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
    document.body.appendChild(tmpNode);
    dpiInfo.dpix = parseInt(tmpNode.offsetWidth);
    dpiInfo.dpiy = parseInt(tmpNode.offsetHeight);
    tmpNode.parentNode.removeChild(tmpNode);
  }
};

function pxConversionMm(value) {
  if (!dpiInfo.dpix) {
    conversion_getDPI()
  }
  var inch = value / dpiInfo.dpix;
  var c_value = inch * 25.4;
//      console.log(c_value);
  return c_value;
};

/**
 * mm转换为px
 * @param value
 * @returns {number}
 */
function mmConversionPx(value) {
  if (!dpiInfo.dpix) {
    conversion_getDPI()
  }
  var inch = value / 25.4;
  var c_value = inch * dpiInfo.dpix;
//      console.log(c_value);
  return c_value;
}


/**
 * 获取元素位置
 * @param {*} element
 */
function cumulativeOffset(element) {
  let top = el.offsetTop
  let left = el.offsetLeft

  return {
    top: top,
    left: left
  }
}

/**
 * 判断元素是否在窗口内
 * @param {*} el
 */
function checkInView(el) {
  let rect = el.getBoundingClientRect()
  return (
    (rect.top > 0 && rect.top < window.innerHeight) &&
    (rect.left < window.innerWidth &&
      rect.right > 0)
  )
}


const YL_VIEW_TYPE = [
  'YL_TYPE_VIEW', 1, 'view',//view (块,线,线框)
  'YL_TYPE_TEXT', 2, 'text',//text (文字, UTF-8格式字符串)
  'YL_TYPE_BARCODE', 3, 'barcode',//barcode (条形码)
  'YL_TYPE_QRCODE', 4, 'qrcode',//qrcode (二维码)
  'YL_TYPE_IMAGE', 5, 'image',//image (图片)
  'YL_TYPE_PATH', 6, 'drawPath'// 绘图路径
]

function getViewDescByType(type) {
  const index = YL_VIEW_TYPE.indexOf(type)
  if (index > 0) {
    return YL_VIEW_TYPE[index + 1]
  }
  return ""
}

function getViewDescByViewType(viewType) {
  const index = YL_VIEW_TYPE.indexOf(viewType)
  if (index > 0) {
    return YL_VIEW_TYPE[index + 2]
  }
  return ""
}

function getViewTypeByType(type) {
  const index = YL_VIEW_TYPE.indexOf(type)
  if (index > 0) {
    return YL_VIEW_TYPE[index - 1]
  }
  return ""
}

function getTypeByViewType(viewType) {
  const index = YL_VIEW_TYPE.indexOf(viewType)
  if (index >= 0) {
    return YL_VIEW_TYPE[index + 1]
  }
  return 0
}

function getModuleViewType() {
  return YL_VIEW_TYPE[0]
}

function deepTrav(obj, handle) {
  _.forOwn(obj, (objc, key) => {
    if (typeof handle === 'function') {
      if (handle(objc, key, obj)) {
        return true
      }
      if (typeof objc === 'object') {
        deepTrav(objc, handle)
      }
    } else {
      return true
    }
  })
}

export {
  pxConversionMm,
  mmConversionPx,
  cumulativeOffset,
  checkInView,
  getViewTypeByType,
  getTypeByViewType,
  getViewDescByType,
  getViewDescByViewType,
  getModuleViewType,
  deepTrav
}
